package io.renren.modules.app.service.impl;

import cn.hutool.core.lang.Snowflake;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.org.apache.bcel.internal.generic.IFEQ;
import io.renren.modules.app.entity.vo.QueryTradeParam;
import io.renren.modules.app.entity.vo.SelectTradeData;
import io.renren.modules.app.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.app.dao.TradeRecordingDao;
import io.renren.modules.app.entity.TradeRecordingEntity;
import io.renren.modules.app.service.TradeRecordingService;
import org.springframework.transaction.annotation.Transactional;


@Service("tradeRecordingService")
public class TradeRecordingServiceImpl extends ServiceImpl<TradeRecordingDao, TradeRecordingEntity> implements TradeRecordingService {

    @Autowired
    private Snowflake snowflake;

    @Autowired
    private TradeRecordingDao tradeRecordingDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TradeRecordingEntity> page = this.page(new Query<TradeRecordingEntity>().getPage(params), new QueryWrapper<TradeRecordingEntity>());

        return new PageUtils(page);
    }

    @Override
    public SelectTradeData queryTradeRecordingList(QueryTradeParam tradeRecording) {
        QueryWrapper<TradeRecordingEntity> wrapper = new QueryWrapper<>();
        if (tradeRecording.getUserId() != null) {
            wrapper.eq("user_id", tradeRecording.getUserId());
        }
        wrapper.orderByDesc("create_time");
        Page<TradeRecordingEntity> iPage = new Page<>(tradeRecording.getPageNum(), tradeRecording.getPageSize());
        IPage<TradeRecordingEntity> cookBookIPage = baseMapper.selectPage(iPage, wrapper);
        List<TradeRecordingEntity> list = cookBookIPage.getRecords();

        SelectTradeData tradeData = new SelectTradeData();
        tradeData.setTotal(cookBookIPage.getTotal());
        tradeData.setCurrentIndex(cookBookIPage.getCurrent());
        tradeData.setSize(cookBookIPage.getSize());
        tradeData.setPageCount(cookBookIPage.getPages());
        tradeData.setTradeRecordingList(list);
        return tradeData;
    }

    @Transactional
    @Override
    public Integer addTradeRecording(TradeRecordingEntity tradeRecording) {
        tradeRecording.setTradeNo(String.valueOf("TY" + snowflake.nextId()));
        tradeRecording.setCreateTime(new Date());
        return baseMapper.insert(tradeRecording);
    }

    /**
     * 统计用户交易次数
     *
     * @param userId    用户id
     * @param tradeType 交易类型(0充值、1提现，2下级返佣)
     * @return
     */
    @Override
    public BigDecimal statisticsUserTrade(Long userId, Integer tradeType) {
        BigDecimal money = tradeRecordingDao.statisticsUserTrade(userId, tradeType);
        if (money==null){
            return new BigDecimal(0.00);
        }else {
            return money;
        }
    }

}